﻿using CodeGeneratorBootstrap.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGeneratorBootstrap.Template.Web.Scripts
{
    public partial class FormJs : ITemplate
    {
        public MetaData Data { get; set; }
        public List<LookupClassModel> lookups { get; set; }
        public List<PickupListModel> pickuplist { get; set; }

        public string Filename
        {
            get { return "form.js"; }
        }

        public bool IsGenerateOneTime { get { return true; } }
        public bool IsGenerateFolder { get { return false; } }
        public bool IsRequiredOnlyOneMember { get { return false; } }
        public GenerateStyle GenerateStyle { get { return GenerateStyle.Normal; } }

        //Calculation Field
        public List<AttributeModel> IsOperandAsCalculateField(AttributeModel currentAtt, ClassModel currentClass)
        {
            List<AttributeModel> ListCalAtt = new List<AttributeModel>();

            string[] separators = { "+", "-", "*", "/", "^", "(", ")", " ", ".concat", ".contain", "AddDate", "AddMonth", "AddYear", "Minimum", "Maximun", "[", "]", "DateDiff", "," };
            string[] words = currentAtt.Formula.Split(separators, StringSplitOptions.RemoveEmptyEntries);

            //int classmodel_id = currentAtt.ClassModel.Id;
            //var currentClass = Data.Models.Where(m => m.Id == classmodel_id).First();

            //Filter for Attribute which is fieldType = 12(Calculation Field)
            List<AttributeModel> Attribute_Calculation = currentClass._attributes.Where(x => x.FieldType.Id == 12).ToList();

            if (Attribute_Calculation != null && Attribute_Calculation.Count() > 0)
            {
                List<string> Att_Calculation_str = new List<string>();
                foreach (var attribute in Attribute_Calculation)
                {
                    Att_Calculation_str.Add("$" + currentClass.Id + "." + attribute.Id);
                }

                var calField = Att_Calculation_str.Intersect(words);

                //Making Return Attributemodel which is use in formula and it's fieldtype = 12 
                if (calField.Count() > 0)
                {
                    foreach (var item in calField)
                    {
                        string[] split = { "$", ".", " " };
                        string[] obj = item.Split(split, StringSplitOptions.RemoveEmptyEntries);

                        //string classid = split[0];
                        string attrid = obj[1];

                        AttributeModel calAttr = currentClass._attributes.Where(m => m.Id == int.Parse(attrid)).First();
                        ListCalAtt.Add(calAttr);
                    }
                }
            }
            return ListCalAtt;
        }

        public List<AttributeModel> GetAttributeForCalculate(ClassModel currentClass)
        {
            List<AttributeModel> ListCalAtt = new List<AttributeModel>();

            foreach (var aAttribute in currentClass._attributes.Where(x => x.FieldType.Id == 12).ToList())
            {
                string[] separators = { "+", "-", "*", "/", "^", "(", ")", " ", ".concat", ".contain", "AddDate", "AddMonth", "AddYear", "Minimum", "Maximun", "[", "]", "DateDiff", "," };
                string[] words = aAttribute.Formula.Split(separators, StringSplitOptions.RemoveEmptyEntries);

                foreach (var item in words)
                {
                    string[] split = { "$", ".", " " };
                    string[] obj = item.Split(split, StringSplitOptions.RemoveEmptyEntries);

                    //string classid = split[0];
                    string attrid = obj[1];

                    AttributeModel calAttr = currentClass._attributes.Where(m => m.Id == int.Parse(attrid)).First();
                    if (!ListCalAtt.Contains(calAttr))
                    {
                        ListCalAtt.Add(calAttr);
                    }
                }
            }
            return ListCalAtt;
        }
    }
}
